package com.example.tdam_2013_g11;

import java.util.ArrayList;
import Adaptadores.ContactoListaAdapter;
import Clases.Contacto;
import Fragments.FragmentInfoContacto;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.content.Intent;
import android.database.Cursor;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

public class InicioActivity extends FragmentActivity {
	ListView listaContactos;
	DrawerLayout panel;
	public static InicioActivity contexto;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.view_inicio);
		contexto = this;
		listaContactos = (ListView) findViewById(R.id.listView_contactos); 
		panel = (DrawerLayout) findViewById(R.id.drawer_layout);		
		cargarListaContactos();
		
		listaContactos.setOnItemClickListener(new ListView.OnItemClickListener() {
           	public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3) {
				try {       
					panel.closeDrawer(Gravity.END);
					Contacto contactoSeleccionado = (Contacto) adapter.getItemAtPosition(position);
					
					Fragment fragmentInfo = new FragmentInfoContacto();
					Bundle argumentos = new Bundle();
					argumentos.putParcelable("Contacto", (Parcelable) contactoSeleccionado);
					fragmentInfo.setArguments(argumentos);
					FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
					transaction.replace(R.id.inicio_fragment_derecha, fragmentInfo);
					transaction.addToBackStack(null);
					transaction.commit();

					panel.openDrawer(Gravity.END);                    
                }
                catch(Exception e) {
                    System.out.println("Nay, cannot get the selected index");
                }
			}
        });

	}
	
	public void onClickHistorial(View view){
    	Intent intent = new Intent(InicioActivity.contexto, HistorialActivity.class);
    	startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.inicio, menu);
		return true;
	}
	
	public void cargarFragmento(Contacto contacto){
	}
	
	public void cargarListaContactos(){
		ArrayList<Contacto> contactos = new ArrayList<Contacto>();
		String[] value = { Phone.DISPLAY_NAME, Phone._ID};
		Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, value , "1=1) GROUP BY (" + Phone.DISPLAY_NAME,null, Phone.DISPLAY_NAME + " ASC" );
        
		while (cursor.moveToNext()) {
			Contacto contacto = new Contacto();
			contacto.setId(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)));
			contacto.setNombre(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));	
			contactos.add(contacto);
		}

		ContactoListaAdapter adaptador = new ContactoListaAdapter(this, R.layout.lista_item_contactos, contactos);        
    	listaContactos.setAdapter(adaptador);
    	adaptador.notifyDataSetChanged();
	}
}
